package com.aduerr.v1shouyin.common;

import android.content.Context;
import com.aduerr.v1shouyin.App;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;

import java.util.List;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2018/10/11 11:20
 * @change
 * @chang time
 * @class describe
 */

public class DatabaseManager {

    public static DatabaseManager mInstance;
    private static LiteOrm liteOrm;
    private static Context mContext = App.getApp();

    private DatabaseManager() {
        liteOrm = LiteOrm.newCascadeInstance(mContext, Constants.DBName);
        liteOrm.setDebugged(false);
    }

    public static DatabaseManager getInstance() {
        if(mInstance == null) {
            synchronized(DatabaseManager.class) {
                if(mInstance == null) {
                    mInstance = new DatabaseManager();
                }
            }
        }
        return mInstance;
    }


    /**
     * 插入一条记录
     *
     * @param t
     */
    public <T> long insert(T t) {
        return liteOrm.save(t);
    }

    public <T> long insertOrReplace(T t) {
        return liteOrm.insert(t, ConflictAlgorithm.Replace);
    }

    /**
     * 插入所有记录
     *
     * @param list
     */
    public <T> void insertAll(List<T> list) {
        liteOrm.save(list);
    }

    public <T> void insertOrReplaceAll(List<T> list) {
        liteOrm.insert(list, ConflictAlgorithm.Ignore);
    }

    /**
     * 查询所有
     *
     * @param cla
     * @return
     */
    public <T> List<T> getQueryAll(Class<T> cla) {
        return liteOrm.query(cla);
    }


    /**
     * 查询所有,column降序
     *
     * @param cla
     * @return
     */
    public <T> List<T> getQueryAllByDesc(Class<T> cla, String column) {

        QueryBuilder<T> qb = new QueryBuilder<>(cla)
                .appendOrderDescBy(column);

        return liteOrm.query(qb);
    }

    /**
     * 查询数量
     *
     * @param cla
     */
    public <T> long queryCount(Class<T> cla) {
        return liteOrm.queryCount(cla);
    }

    /**
     * 查询  某字段 等于 Value的值
     *
     * @param cla
     * @param field
     * @param value
     * @return
     */
    public <T> List<T> getQueryByWhere(Class<T> cla, String field, String[] value) {
        return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value));
    }

    /**
     * 查询  某字段 等于 Value的值  可以指定从1-20，就是分页
     *
     * @param cla
     * @param field
     * @param value
     * @param start
     * @param length
     * @return
     */
    public <T> List<T> getQueryByWhereLength(Class<T> cla, String field, String[] value
            , int start, int length) {
        return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value).limit(start, length));
    }

    /**
     * 条件筛选
     *
     * @param cla
     * @param field
     * @param start
     * @param end
     * @param <T>
     * @return
     */
    public <T> List<T> getQueryByCondition(Class<T> cla, String field, String start,
                                           String end) {
        return liteOrm.<T>query(new QueryBuilder(cla)
                .whereGreaterThan(field, start)
                .whereAppendAnd()
                .whereLessThan(field, end))
                ;
    }

    /**
     * 删除所有 某字段等于 Vlaue的值
     *
     * @param cla
     * @param field
     * @param value
     */
    public <T> void deleteWhere(Class<T> cla, String field, String[] value) {
        liteOrm.delete(cla, WhereBuilder.create(cla).where(field + "=?", value));
    }

    /**
     * 删除所有
     *
     * @param cla
     */
    public <T> void deleteAll(Class<T> cla) {
        liteOrm.deleteAll(cla);
    }

    /**
     * 删除第一项
     *
     * @param cla
     */
    public <T> void deleteFirst(Class<T> cla, String column) {
        // 最后一个参数可为null，默认按ID升序排列
        // 按column升序，删除[2, size-1]，结果：仅保留第一个和最后一个
        liteOrm.delete(cla, 1, 1, column);

    }

    /**
     * 仅在以存在时更新
     *
     * @param t
     */
    public <T> void update(T t) {
        liteOrm.update(t, ConflictAlgorithm.Replace);
    }

    public <T> void updateByWhere(Class<T> cla, String where, String whereArgs,
                                  String colume, Object value) {
        int result = liteOrm.update(WhereBuilder.create(cla).where(where + "=?",
                new String[]{whereArgs}), new ColumnsValue(new String[]{colume},
                new Object[]{value}), ConflictAlgorithm.Replace);
    }

    public <T> void updateALL(List<T> list) {
        liteOrm.update(list);
    }

}
